﻿Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class EdgeBarPage
    Inherits System.Windows.Forms.NativeWindow
    Implements System.IDisposable

    Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean

    Private _edgeBar As SolidEdgeFramework.ISolidEdgeBarEx
    Private _document As SolidEdgeFramework.SolidEdgeDocument
    Private _childControl As System.Windows.Forms.Control

    Public Sub New(edgeBar As SolidEdgeFramework.ISolidEdgeBarEx, document As SolidEdgeFramework.SolidEdgeDocument, childConrol As System.Windows.Forms.Control, resID As Int32)
        Dim strResPath As String
        Dim hWnd As Int32

        _edgeBar = edgeBar
        _document = document
        _childControl = childConrol

        strResPath = Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, "Resources.dll")

        'NO_RESIZE_CHILD = 1,
        'DONOT_MAKE_ACTIVE = 2,
        hWnd = _edgeBar.AddPageEx(_document, strResPath, resID, "My tooltip", 2)
        AssignHandle(New IntPtr(hWnd))
        SetParent(childConrol.Handle, Handle)
        'SW_SHOWMAXIMIZED = 3,
        ShowWindow(childConrol.Handle, 3)
    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        Dim hWnd As IntPtr

        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
            hWnd = Handle
            ReleaseHandle()
            Try
                _edgeBar.RemovePage(_document, hWnd.ToInt32(), 0)
                Marshal.ReleaseComObject(_document)
                Marshal.ReleaseComObject(_edgeBar)
            Catch ex As Exception

            End Try

        End If

        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
